/*
 * MIT License
 *
 * Copyright (c) 2021 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.seaboot.excel;

import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStrings;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.Consumer;

/**
 * 07 版本的 Excel 工具
 * <p>
 * HSSFWorkbook 03 版本，XSSFWorkbook 07 版本
 * <p>
 * 测试版本，基于 SAX 解析实现，还未攒足经验，特殊的文档可能会出现兼容问题
 *
 * @author Mr.css
 * @version 2022-02-24 10:44
 * @since 2022-12-15 删除泛型，避免产生奇怪的代码警告，使用的时候需要注意对象类型的设置
 */
public class SaxExcelReader {

    public void process(File file, Consumer<List<String>> consumer) throws OpenXML4JException, IOException, ParserConfigurationException, SAXException {
        try (OPCPackage pkg = OPCPackage.open(file);) {
            XSSFReader reader = new XSSFReader(pkg);
            XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) reader.getSheetsData();

            // 4. 获取共享字符串表和样式表
            SharedStrings sst = reader.getSharedStringsTable();
            while (sheets.hasNext()) {
                // read first sheet
                try (InputStream is = sheets.next()) {
                    InputSource sheetSource = new InputSource(is);
                    sheetSource.setEncoding("UTF-8");
                    XMLReader parser = XMLHelper.newXMLReader();
//
                    SheetHandler handler = new SheetHandler();
                    handler.setConsumer(consumer);
                    handler.setSharedStrings(sst);

                    parser.setContentHandler(handler);
                    parser.parse(sheetSource);
                }
            }
        }
    }
}

